﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using client.Settings;

namespace client
{
    /// <summary>
    /// Interaction logic for Preferences.xaml
    /// </summary>
    public partial class Preferences : Window
    {
        const string name = "GNSClient";
        private string userLogin;
        private string userCultureInfo;
        private string olduserCultureInfo;
        private bool startMinimized;
        private bool enableNotifyWindow;
        private int temperatureThreshold;
        private int pressureThreshold;
        private string tempUserLangInfo;
        ConfigSettings conf = new ConfigSettings();
               
        public Preferences()
        {
            InitializeComponent();
            this.MouseLeftButtonDown += delegate { DragMove(); }; 
        }
      
       
        /// <summary>
        /// Write user login, language, autoload options to the configuration file app.set
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            userLogin = textBoxUserLogin.Text;
            conf.getLogin = userLogin;
            try
            {
                userCultureInfo = comboLanguage.SelectionBoxItem.ToString();
            }
            catch (NullReferenceException)
            {
            }
            if (userCultureInfo != olduserCultureInfo)
                MessageBox.Show(Languages.Language.changedLanguage.ToString());
            if (startWithWindows.IsChecked == true)
            {
                SetAutorunValue(true);
            }
            else if (startWithWindows.IsChecked == false)
            {
                SetAutorunValue(false);
            }
            conf.getLoadWithSystem = startWithWindows.IsChecked.Value;
            conf.getUserCultureInfo = userCultureInfo;
            conf.getStartMinimized = startMinimizedKey.IsChecked.Value;
            conf.getEnableNotifyWindow = enableNotifyWindowKey.IsChecked.Value;
            conf.getTimeDelay = Convert.ToInt32(cmbDelay.SelectionBoxItem);
            try
            {
                conf.Temp = Convert.ToInt32(textBoxTemperature.Text);
                UserSettings.tempThreshold = Convert.ToInt32(textBoxTemperature.Text);
            }
            catch (FormatException)
            {
                conf.Temp = 0;
                UserSettings.tempThreshold = 0;
            }
            try
            {
                conf.Pressure = Convert.ToInt32(textBoxPressure.Text);
                UserSettings.pressureThreshold = Convert.ToInt32(textBoxPressure.Text);
            }
            catch (FormatException)
            {
                conf.Pressure = 0;
                UserSettings.pressureThreshold = 0;
            }
            enableNotifyWindow = enableNotifyWindowKey.IsChecked.Value;
            UserSettings.UserName = userLogin;
            UserSettings.Language = userCultureInfo;
            UserSettings.loadWithSystem = startWithWindows.IsChecked.Value;
            UserSettings.startMinimized = startMinimizedKey.IsChecked.Value;
            UserSettings.enableNotifyWindow = enableNotifyWindowKey.IsChecked.Value;
            UserSettings.timeDelay = Convert.ToInt32(cmbDelay.SelectionBoxItem);
            Managers.Serialization.serializationPref(conf);

        }
       
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.RegistryKey myKey;
            try
            {
                myKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run\");
                string loadString = (string)myKey.GetValue(name);
                if (loadString == null)
                {
                    startWithWindows.IsChecked = false;
                }
                else startWithWindows.IsChecked = true;
                myKey.Close();
            }
            catch (Exception ex)
            { }
            try
            {
                userLogin = UserSettings.UserName;
                textBoxUserLogin.Text = userLogin;
                startMinimizedKey.IsChecked = UserSettings.startMinimized;
                enableNotifyWindowKey.IsChecked = UserSettings.enableNotifyWindow;
                textBoxTemperature.Text = UserSettings.tempThreshold.ToString();
                textBoxPressure.Text = UserSettings.pressureThreshold.ToString();
                userCultureInfo = UserSettings.Language;
                cmbDelay.SelectedItem = cmbDelay.FindName("i"+UserSettings.timeDelay.ToString());
                olduserCultureInfo = UserSettings.Language;
                comboLanguage.SelectedItem = comboLanguage.FindName(UserSettings.Language.ToString());
            }
            catch (NullReferenceException ex)
            {
            }     
        }

        private bool SetAutorunValue(bool autorun)
        {
            string ExePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            Microsoft.Win32.RegistryKey myKey;
            myKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\");
            try
            {
                if (autorun)
                    myKey.SetValue(name, ExePath);
                else
                    myKey.DeleteValue(name);

                myKey.Close();
            }
            catch
            {
                return false;
            }
            return true;
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

    }
}
